﻿using System;
using System.Data;
using System.Windows.Forms;
using FoundationClass;
using FoundationClass.Entity;
using Telerik.WinControls.UI;

namespace P2MARY_INTERNET.Controls
{
    public partial class OverAllForm : UserControl
    {
        public OverAllForm()
        {
            InitializeComponent();
        }

        public User UserLogin { get; set; }
        private void OverAllForm_Load(object sender, EventArgs e)
        {
            SetupGrid();
        }

        private void SetupGrid()
        {
            try
            {
                var promotions = MasterDataHandler.GetAllPromotions();
                drpPromotion.Items.Clear();
                foreach (Promotion t in promotions)
                {
                    var item = new RadListDataItem(t.Name, t);
                    drpPromotion.Items.Add(item);
                }

                var computers = MasterDataHandler.ListUnusedComputer();
                ddlComputerList.Items.Clear();
                foreach (Computer c in computers)
                {
                    var item = new RadListDataItem(c.Id.ToString(), c);
                    ddlComputerList.Items.Add(item);
                }
                ddlComputerList.SelectedIndex = 0;
                drpPromotion.SelectedIndex = 0;

                var dt = new DataTable();
                dt.Columns.Add("serviceid", typeof(string));
                dt.Columns.Add("comNo", typeof(string));
                dt.Columns.Add("type", typeof(string));
                dt.Columns.Add("promotion", typeof(string));
                dt.Columns.Add("status", typeof(string));
                dt.Columns.Add("start", typeof(string));
                dt.Columns.Add("finish", typeof(string));
                dt.Columns.Add("amount", typeof(string));

                var list = MasterDataHandler.GetAllServiceRecordsAvaliable();
                foreach (ServiceRecord sr in list)
                {
                    string status = "Open";
                    if (sr.RecordStatus == 2)
                        status = "Close";

                    string type = "Internet";
                    if (sr.Computer.ComputerType == 'G')
                        type = "Games";

                    dt.Rows.Add(sr.Id, sr.Computer.Id.ToString(), type, promotion.Name, status, sr.RecordStart.ToLongTimeString(), sr.RecordEnd.HasValue ? sr.RecordEnd.Value.ToLongTimeString() : string.Empty, sr.RecordAmount);
                }
                grdComUsage.DataSource = dt;
            }
            catch (Exception ex)
            {
                //MessageBox.Show(@"OverAll SetupGrid :" + ex.Message);
            }
        }


        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (ddlComputerList.SelectedItem == null || drpPromotion.SelectedItem == null)
                    return;
                var service = new ServiceRecord
                    {
                        Computer = ddlComputerList.SelectedItem.Value as Computer,
                        Promotion = drpPromotion.SelectedItem.Value as Promotion,
                        RecordStart = DateTime.Now,
                        RecordStatus = 1
                    };
                MasterDataHandler.AddSevicesRecord(service);

                SetupGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Overall Start: "+ex.Message);
            }
        }

        private void grdComUsage_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 0)
                {
                    string status = grdComUsage.Rows[e.RowIndex].Cells["status"].Value.ToString();
                    if (string.IsNullOrEmpty(status) || status.Equals("Open"))
                    {
                        int id = int.Parse(grdComUsage.Rows[e.RowIndex].Cells["serviceid"].Value.ToString());
                        var serviceRecord = MasterDataHandler.GetServiceRecord(id);
                        serviceRecord.RecordEnd = DateTime.Now;
                        serviceRecord.RecordStatus = 2;
                        serviceRecord.Payment = new Payment
                                                    {
                                                        Employee = UserLogin,
                                                        TotalAmount = serviceRecord.CalculateTotalAmount()
                                                    };
                        serviceRecord.Payment.AddPayment();

                        MasterDataHandler.UpdateSevicesRecord(serviceRecord);
                        SetupGrid();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Overall grdCellClick : "+ex.Message);
            }
        }
    }
}
